package com.dzu.easy;

import com.dzu.common.ListNode;

/**
 * @author ZhaoDong
 * @date 2023/1/10 10:36
 * @description 206. 反转链表
 */
public class Test206 {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        ListNode l5 = new ListNode(5);

        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;

        System.out.println(reverseList(l1));
    }

    public static ListNode reverseList2(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }

    public static ListNode reverseList(ListNode head) {
        ListNode resp = new ListNode(-1);
        ListNode temp = head;
        while (temp != null) {

            ListNode ln = resp.next;
            resp.next = temp;

            temp = temp.next;
            resp.next.next = ln;
        }
        return resp.next;
    }
}
